package com.spzx.user.service.impl;

import com.spzx.user.service.SMSService;
import com.spzx.user.utils.SMSUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SMSServiceImpl implements SMSService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void sendCode(String phone) {
        //生成6位的随机字符串作为验证码
        String code = RandomStringUtils.randomNumeric(4);
        //向手机发送手机验证码
        SMSUtils.sendCode(phone,code);

        System.out.println(code);
        //将验证码保存到Redis中,并设置时间
        stringRedisTemplate.opsForValue().set("h5:"+phone,code,5, TimeUnit.MINUTES);

    }
}
